package com.ruoyi.douyin.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.douyin.vo.DouyinVideoVo;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.DouyinVideo;
import com.ruoyi.system.domain.vo.DouyinVideoTotalVo;
import com.ruoyi.system.service.DouyinVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/douyin")
public class DouyinVideoController {

    @Autowired
    private DouyinVideoService douyinVideoService;
    @Autowired
    private TokenService tokenService;

    /**
     * 获取本地视频的列表
     *
     * @param douyinVideoVos openId
     * @param index          页码
     * @ 时间
     * @ 标签
     * @ 分组
     * @ 运营人
     */
    @PostMapping("/getVideoList/{index}")
    public AjaxResult getVideoList(@RequestBody DouyinVideoVo douyinVideoVos,
                                   @PathVariable("index") int index) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        PageHelper.startPage(index, 10);
        List<DouyinVideo> videoList = null;
        if (douyinVideoVos.getStart() == null) {
            videoList = douyinVideoService.getVideoList(douyinVideoVos.getOpenId(),
                    loginUser.getUser().getUserId().toString());
        } else {
            videoList = douyinVideoService.getVideoListByData(douyinVideoVos.getOpenId(),
                    loginUser.getUser().getUserId().toString(),
                    douyinVideoVos.getStart(), douyinVideoVos.getEnd());
        }
        return AjaxResult.success(PageInfo.of(videoList));
    }

    /**
     * @param openIds openId
     * @return
     */
    @PostMapping("/getVideoNum")
    public AjaxResult getVideoNum(@RequestBody List<String> openIds) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        DouyinVideoTotalVo douyinVideoTotalVo = douyinVideoService.totalNum(openIds,
                loginUser.getUser().getUserId().toString());
        return AjaxResult.success(douyinVideoTotalVo);
    }
}
